package com.uex.dev.manage.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Preconditions;
import com.uex.dev.manage.admin.dao.ProblemDao;
import com.uex.dev.manage.admin.po.MaintainPO;
import com.uex.dev.manage.admin.po.ProblemPO;
import com.uex.dev.manage.admin.service.IMaintainService;
import com.uex.dev.manage.admin.service.IProblemService;
import com.uex.dev.manage.admin.utils.LogUtil;
import com.uex.dev.manage.admin.vo.problem.ProblemInfoResp;
import com.uex.dev.manage.admin.vo.problem.ProblemMaintainInfo;
import com.uex.dev.manage.common.base.PageResult;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 客户问题反馈表 服务实现类
 * </p>
 *
 * @author wangchao
 * @since 2024-02-26
 */
@Service
public class ProblemServiceImpl extends ServiceImpl<ProblemDao, ProblemPO> implements IProblemService {

    @Resource
    private IMaintainService maintainService;

    @Override
    public PageResult<ProblemInfoResp> selectList(Integer deviceId, Integer page, Integer record) {
        Page<ProblemPO> reusltPage = new Page<>(page, record);
        LambdaQueryWrapper<ProblemPO> wrapper = Wrappers.<ProblemPO>lambdaQuery()
                .eq(Objects.nonNull(deviceId), ProblemPO::getProblemDevid, deviceId)
                .orderByDesc(ProblemPO::getId);
        Page<ProblemPO> result = this.page(reusltPage, wrapper);
        PageResult<ProblemInfoResp> pageResult = PageResult.build(result).convert(ProblemInfoResp::fromPO);
        LogUtil.log(deviceId != null ? deviceId : 0, "查看问题列表");
        return pageResult;
    }

    @Override
    public ProblemInfoResp getDetail(Integer problemId) {
        Preconditions.checkArgument(ObjectUtils.isNotNull(problemId), "问题Id不能为空");
        ProblemPO problem = this.getById(problemId);
        Preconditions.checkArgument(ObjectUtils.isNotNull(problemId), "问题Id不存在记录");
        ProblemInfoResp problemInfoResp = ProblemInfoResp.fromPO(problem);
        LogUtil.log(problem.getProblemDevid(), "查看问题详情");
        return problemInfoResp;
    }

    @Override
    public PageResult<ProblemMaintainInfo> getMaintain(Integer problemId, Integer page, Integer record) {
        LambdaQueryWrapper<MaintainPO> wrapperByIds = Wrappers.<MaintainPO>lambdaQuery()
                .apply(Objects.nonNull(problemId),"JSON_CONTAINS(maintain_problem_ids, {0})", problemId+"")
                .orderByDesc(MaintainPO::getId);
        Page<MaintainPO> resultPage = new Page<>(page, record);
        Page<MaintainPO> pageResult = maintainService.page(resultPage, wrapperByIds);
        PageResult<ProblemMaintainInfo> result = PageResult.build(pageResult).convert(ProblemMaintainInfo::fromPO);
        LogUtil.log(0, "查看问题维修记录及详情");
        return result;
    }

    @Override
    public void updateProblem(ProblemPO problemDetailInfo) {
        if (ObjectUtils.isNull(problemDetailInfo.getId()) || problemDetailInfo.getId() == 0) {
            Preconditions.checkArgument(ObjectUtils.isNotNull(problemDetailInfo.getProblemDevid()), "设备Id不能为空");
            Preconditions.checkArgument(ObjectUtils.isNotNull(problemDetailInfo.getProblemPrjid()), "项目Id不能为空");
            Preconditions.checkArgument(ObjectUtils.isNotNull(problemDetailInfo.getProblemStatus()), "问题状态不正确");
            problemDetailInfo.setProblemTime(LocalDateTime.now());
            problemDetailInfo.setProblemUpdtime(LocalDateTime.now());
            this.save(problemDetailInfo);
        } else {
            problemDetailInfo.setProblemUpdtime(LocalDateTime.now());
            Preconditions.checkArgument(this.updateById(problemDetailInfo), "问题Id不存在");
        }
        LogUtil.log(problemDetailInfo.getProblemDevid(), "添加或更新问题记录");
    }

    @Override
    public void updateMaintain(MaintainPO maintainUpdateReq) {
        if (ObjectUtils.isNull(maintainUpdateReq.getId()) || maintainUpdateReq.getId() == 0) {
            Preconditions.checkArgument(ObjectUtils.isNotNull(maintainUpdateReq.getMaintainProblemIds()), "问题列表不能为空");
            Preconditions.checkArgument(ObjectUtils.isNotNull(maintainUpdateReq.getMaintainTime()), "维修时间不能为空");
            Preconditions.checkArgument(ObjectUtils.isNotNull(maintainUpdateReq.getMaintainType()), "维修类型不能为空");
            List<Integer> problemIds = maintainUpdateReq.getMaintainProblemIds();
            ArrayList<Integer> errIds = new ArrayList<>();
            List<ProblemPO> list = this.list();
            ArrayList<Integer> matchList = new ArrayList<>();
            list.forEach(x -> matchList.add(x.getId()));
            problemIds.forEach(x -> {
                if (!matchList.contains(x)) {
                    errIds.add(x);
                }
            });
            String errMsg = errIds.toString();
            Preconditions.checkArgument(errIds.isEmpty(), String.format("问题Id %s", errMsg));
            maintainService.save(maintainUpdateReq);
        } else {
            Preconditions.checkArgument(maintainService.updateById(maintainUpdateReq), "维护Id不存在");
        }
        LogUtil.log(0, "添加或更新维修记录");
    }
}
